<script setup lang="ts">
import { getMyLikes } from '@/api/post'
import { ref } from 'vue'

const likes = ref([])

// 获取点赞列表
async function fetchLikes() {
  const res = await getMyLikes()
  likes.value = res.data
}

// 跳转到详情页
function goToPostDetail(postId: number) {
  uni.navigateTo({
    url: `/pages/post/detail/index?id=${postId}`,
  })
}

// 初始化加载
fetchLikes()
</script>

<template>
  <view class="likes-container">
    <view class="list">
      <view
        v-for="(item, index) in likes"
        :key="index"
        class="like-item"
        @click="goToPostDetail(item.postId)"
      >
        <view class="post-header">
          <image class="avatar" :src="item.post?.user?.avatar || '/static/avatar.jpg'" />
          <text class="username">
            {{ item.user?.nickname || '匿名用户' }}
          </text>
        </view>
        <view class="post-content">
          <text class="title">
            {{ item.post?.title }}
          </text>
          <text class="excerpt">
            {{ item.post?.content?.substring(0, 50) }}...
          </text>
        </view>
        <view class="post-footer">
          <text class="time">
            {{ item.createdAt }}
          </text>
        </view>
      </view>
    </view>
  </view>
</template>

<style lang="scss">
.likes-container {
  padding: 20rpx;
  background-color: #f5f5f5;

  .like-item {
    padding: 30rpx;
    margin-bottom: 20rpx;
    background: #fff;
    border-radius: 16rpx;
    box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);

    .post-header {
      display: flex;
      align-items: center;
      margin-bottom: 20rpx;

      .avatar {
        width: 60rpx;
        height: 60rpx;
        border-radius: 50%;
        margin-right: 20rpx;
      }

      .username {
        font-size: 28rpx;
        color: #666;
      }
    }

    .post-content {
      margin-bottom: 20rpx;

      .title {
        font-size: 32rpx;
        font-weight: bold;
        color: #333;
        display: block;
        margin-bottom: 10rpx;
      }

      .excerpt {
        font-size: 28rpx;
        color: #666;
        line-height: 1.5;
      }
    }

    .post-footer {
      .time {
        font-size: 24rpx;
        color: #999;
      }
    }
  }
}
</style>
